﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Xml;

namespace Xinnos.Lca.Yacht.Analyzer.View
{
    internal class WorkBreakdownStructureViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public ObservableCollection<WorkBreakdownStructureViewModel> Children { get; private set; }
        protected string mCode = null;
        protected string mName = null;
        protected WorkBreakdownStructureViewModel mParent = null;
        protected WorkBreakdownStructureGroupViewModel mGroup = null;
        protected bool mSelected = false;

        public string Code
        {
            get { return mCode; }
            set
            {
                mCode = value;
                RaisePropertyChanged("Code");
            }
        }
        public string Name
        {
            get { return mName; }
            set
            {
                mName = value;
                RaisePropertyChanged("Name");
            }
        }
        public WorkBreakdownStructureViewModel Parent
        {
            get { return mParent; }
            set
            {
                mParent = value;
                RaisePropertyChanged("Parent");
            }
        }
        public WorkBreakdownStructureGroupViewModel Group
        {
            get { return mGroup; }
            set
            {
                mGroup = value;
                RaisePropertyChanged("Group");
            }
        }
        public bool Selected
        {
            get { return mSelected; }
            set
            {
                mSelected = value;
                RaisePropertyChanged("Selected");
            }
        }

        public WorkBreakdownStructureViewModel()
        {
            if (null == Children) Children = new ObservableCollection<WorkBreakdownStructureViewModel>();
        }

        public void AddChild(WorkBreakdownStructureViewModel vmWbs)
        {
            if (null != vmWbs)
            {
                Children.Add(vmWbs);
                vmWbs.Parent = this;
                vmWbs.Group = this.Group;
            }
        }

        public virtual XmlElement Export(XmlDocument doc)
        {
            XmlElement _element = null;
            if (null != doc)
            {
                _element = doc.CreateElement("WorkBreakdownStructure");
                if (!string.IsNullOrEmpty(this.Code))
                {
                    XmlAttribute _code = doc.CreateAttribute("Code");
                    _code.Value = this.Code;
                    _element.Attributes.Append(_code);
                }
                if (!string.IsNullOrEmpty(this.Name))
                {
                    XmlAttribute _name = doc.CreateAttribute("Name");
                    _name.Value = this.Name;
                    _element.Attributes.Append(_name);
                }
                if (0 < Children.Count)
                {
                    XmlElement _children = doc.CreateElement("Children");
                    foreach (WorkBreakdownStructureViewModel _child in Children) _children.AppendChild(_child.Export(doc));
                    _element.AppendChild(_children);
                }
            }
            return _element;
        }

        public virtual void Import(XmlElement element){
            if (null == element) return;
            if ("WorkBreakdownStructure" == element.Name)
            {
                if (element.HasAttribute("Code")) this.Code = element.Attributes["Code"].Value;
                else this.Code = null;
                if (element.HasAttribute("Name")) this.Name = element.Attributes["Name"].Value;
                else this.Name = null;
                Children.Clear();
                if (element.HasChildNodes)
                {
                    foreach (XmlNode _children in element.ChildNodes)
                    {
                        if ("Children" == _children.Name)
                        {
                            if (_children.HasChildNodes)
                            {
                                ConfigurationViewModel _configuration = ConfigurationViewModel.GetInstance();
                                foreach (XmlNode _child in _children)
                                {
                                    WorkBreakdownStructureViewModel _model = null;
                                    if ("ObjectViewModel" == this.GetType().Name) _model = new ObjectViewModel();
                                    else _model = new WorkBreakdownStructureViewModel();
                                    _model.Import(_child as XmlElement);
                                    this.AddChild(_model);
                                }
                            }
                        }
                    }
                }
            }
        }

        protected void RaisePropertyChanged(string strPropertyName)
        {
            PropertyChangedEventHandler _handler = this.PropertyChanged;
            if (null != _handler) _handler(this, new PropertyChangedEventArgs(strPropertyName));
        }
    }
}